package cn.ffcs.uec.staff.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.staff.action.bean.UecHightTeamEditBean;
import cn.ffcs.uec.staff.model.UecHightTeam;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;

/**
 * 高技能人才队伍
 * 
 * @Title:
 * @Description:
 * @Copyright: FFCS（C） 2017
 * @Company: FFCS
 * @author Wang Yiren
 * @Version 0.0.1 date:2018年1月7日
 * @since JDK1.6
 */
@Controller
@Scope("prototype")
public class UecHightTeamEditComposer extends BasePortletComposer {

	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private UecHightTeamEditBean bean = new UecHightTeamEditBean();
	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 人员信息
	 */
	private UecStaffInfo uecStaffInfo;
	/**
	 * 添加高技能人才队伍
	 */
	private UecHightTeam uecHightTeam;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化窗口
	 * 
	 * @throws ParseException
	 */
	public void onCreate$uecHightTeamEditWindow() throws ParseException {
		this.bindCombobox();
		this.bindBean();
	}

	/**
	 * 绑定combobox
	 */
	private void bindCombobox() {
		// 高技能人才名称glbdef1
		List<NodeVo> nodeVoGlbdef1List = new ArrayList<NodeVo>();
		String[][] glbdef1 = { { "1001A1100000000005OW", "销售技能型" },
				{ "1001A1100000000005OX", "服务技能型" },
				{ "1001A1100000000005OY", "维护技能型" } };
		for (int i = 0; i < glbdef1.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(glbdef1[i][j]);
				nodeVo.setName(glbdef1[i][j + 1]);
			}
			nodeVoGlbdef1List.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getGlbdef1(), nodeVoGlbdef1List);

		// 年度考核结果glbdef2
		List<NodeVo> nodeVoGlbdef2List = new ArrayList<NodeVo>();
		String[][] glbdef2 = { 
				{ "1001A11000000000061J", "优秀" },
				{ "1001A11000000000061K", "称职" },
				{ "1001A11000000000061M", "基本称职" }, 
				{ "1001A11000000000061N", "不称职" }, 
				{ "1001A11000000000061O", "反应层评估" }, 
				{ "1001A11000000000061P", "学习层评估" }, 
				{ "1001A11000000000061Q", "行为层评估" },
				{ "1001A11000000000061R", "结果层评估" }
		};
		for (int i = 0; i < glbdef2.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(glbdef2[i][j]);
				nodeVo.setName(glbdef2[i][j + 1]);
			}
			nodeVoGlbdef2List.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getGlbdef2(), nodeVoGlbdef2List);

	}

	/**
	 * bindBean
	 * 
	 * @throws ParseException
	 */
	public void bindBean() throws ParseException {
		opType = (String) arg.get("opType");
		if ("add".equals(opType)) {
			this.bean.getUecHightTeamEditWindow().setTitle("新增高技能人才队伍");
			bean.getUecHightTeamEditDiv().setHeight("30%");
			uecStaffInfo = (UecStaffInfo) arg.get("uecStaffInfo");
		}
		if ("view".equals(opType)) {
			this.bean.getUecHightTeamEditWindow().setTitle("查看高技能人才队伍");
			uecHightTeam = (UecHightTeam) arg.get("uecHightTeam");
			if (null != uecHightTeam) {
				PubUtil.fillBeanFromPo(uecHightTeam, bean);
				stringTransToDate();
				bean.getButtonDiv().setVisible(false);
				PubUtil.disableComponent(bean.getUecHightTeamEditWindow(), true);
			}
		}
		if ("edit".equals(opType)) {
			this.bean.getUecHightTeamEditWindow().setTitle("编辑高技能人才队伍");
			bean.getUecHightTeamEditDiv().setHeight("30%");
			uecHightTeam = (UecHightTeam) arg.get("uecHightTeam");
			if (null != uecHightTeam) {
				PubUtil.fillBeanFromPo(uecHightTeam, bean);
				stringTransToDate();
			}
		}
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		if ("add".equals(opType)) {
			uecHightTeam = UecHightTeam.newInstance();
			PubUtil.fillPoFromBean(bean, uecHightTeam);
			dateTransToString();
			uecHightTeam.setStaffId(uecStaffInfo.getStaffId());
			uecHightTeam.addOnly();
			Events.postEvent("onOK", this.self, uecHightTeam);
			this.onCancel();
			Messagebox.show("新增成功！");
		}
		if ("edit".equals(opType)) {
			PubUtil.fillPoFromBean(bean, uecHightTeam);
			dateTransToString();
			uecHightTeam.updateOnly();
			Events.postEvent("onOK", this.self, uecHightTeam);
			this.onCancel();
			Messagebox.show("更新成功！");
		}
	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getUecHightTeamEditWindow().onClose();
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		// if (StrUtil.isNullOrEmpty(bean.getEncourName().getValue())) {
		// return "填写奖励名称";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourDate().getValue())) {
		// return "填写奖励日期";
		// }
		return null;
	}

	/**
	 * 将Date类型时间转成String类型 Date --> String 一套模型，两套标准：日期类型需要格式化
	 */
	public void dateTransToString() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		// 开始日期
		if (!StrUtil.isNullOrEmpty(bean.getBegindate().getValue())) {
			uecHightTeam.setBegindate(formatter.format(bean.getBegindate()
					.getValue()));
		}
		// 结束日期
		if (!StrUtil.isNullOrEmpty(bean.getEnddate().getValue())) {
			uecHightTeam.setEnddate(formatter.format(bean.getEnddate()
					.getValue()));
		}

	}

	/**
	 * 将String类型的时间转换成Date类型
	 * 
	 * @throws ParseException
	 */
	public void stringTransToDate() throws ParseException {
		// 时间类型转换：String --> Date
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		// 开始日期
		if (null != uecHightTeam.getBegindate()) {
			bean.getBegindate()
					.setValue(sdf.parse(uecHightTeam.getBegindate()));
		}
		// 结束日期
		if (null != uecHightTeam.getEnddate()) {
			bean.getEnddate().setValue(sdf.parse(uecHightTeam.getEnddate()));
		}
	}

}
